package com.normalsys.live.base.util.file;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: 刘天宇
 * Date: 2018/8/14
 * Time: 21:00
 * Description:
 */
public class ImgUtil {
    private ImgUtil(){}

    /**
     * 压缩文件
     * @param inputStream  文件流
     * @param format       文件格式  jpg jepg等
     * @return
     */
    public static InputStream getFileCompressStream(InputStream inputStream, String format) {
        InputStream is = null;
        try {
            BufferedImage bufferedImage= Thumbnails.of(inputStream)
                    .scale(1f).outputQuality(0.5f).asBufferedImage();

            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(bufferedImage, format, imOut);
            is = new ByteArrayInputStream(bs.toByteArray());
        } catch (IOException e) {
            System.out.println("压缩文件异常");
            e.printStackTrace();
        }
        return is;
    }
}
